package com.example.smartcity1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.NewsCommentsBean;

import java.util.List;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class NewsCommentsAdapter extends ArrayAdapter<NewsCommentsBean.RowsDTO> {

    public NewsCommentsAdapter(@NonNull Context context, int resource, @NonNull List<NewsCommentsBean.RowsDTO> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_news_comments, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.itemNewsComImg = convertView.findViewById(R.id.item_news_com_img);
            viewHolder.itemNewsComName = convertView.findViewById(R.id.item_news_com_name);
            viewHolder.itemNewsComTime = convertView.findViewById(R.id.item_news_com_time);
            viewHolder.itemNewsComContent = convertView.findViewById(R.id.item_news_com_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        NewsCommentsBean.RowsDTO data = getItem(position);
        if (data != null) {
            Glide.with(getContext()).load(IP + data.getAvatar())
                    .centerCrop()
                    .into(viewHolder.itemNewsComImg);
            viewHolder.itemNewsComName.setText(data.getUserName());
            viewHolder.itemNewsComTime.setText(data.getCreateTime());
            viewHolder.itemNewsComContent.setText(data.getContent());
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView itemNewsComImg;
        TextView itemNewsComName;
        TextView itemNewsComTime;
        TextView itemNewsComContent;
    }
}
